package com.team7.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team7.order.dao.CourseDao;
import com.team7.order.entity.Course;
import com.team7.order.service.CourseService;
import com.team7.order.utils.CommonResult;
import com.team7.order.vo.CourseDetailsVo;
import com.team7.order.vo.KeShiVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 张煜杰
 * @since 2021-10-22
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseDao, Course> implements CourseService {

    @Resource
    private CourseDao courseDao;
    @Resource
    private RedisTemplate redisTemplate;


    //  通过courseId查询所有的课程信息
    public CommonResult queryAllCourse(String courseId) {
        List<Course> courses = courseDao.queryCourseById(courseId);
        return new CommonResult(2000,"成功",courses);
    }

    //    通过课程id和讲师id查询课程和讲师信息
    @Override
    public CommonResult queryAllCourseAndTeacherName(String id) {
//        Object o = redisTemplate.opsForValue().get("queryAllCourseAndTeacherName::" + id);
//        if (o!=null){
//            if (o instanceof NullObject){
//                return null;
//            }else{
//                return new CommonResult(2000,"成功",o);
//            }
//        }
//        CourseDetailsVo courseDetailsVo = courseDao.queryAllCourseAndTeacherName(id);
//        if (courseDetailsVo==null){
//            redisTemplate.opsForValue().set("queryAllCourseAndTeacherName::"+id,new NullObject(),1,TimeUnit.MINUTES);
//        }else{
//            redisTemplate.opsForValue().set("queryAllCourseAndTeacherName::"+id,courseDetailsVo);
//        }
        CourseDetailsVo courseDetailsVo = courseDao.queryAllCourseAndTeacherName(id);
//        浏览数量+1
        Long VierCount = courseDetailsVo.getViewCount()+1;
        Course course = new Course();
        course.setId(id);
        course.setViewCount(VierCount);
        courseDao.updateById(course);

        return new CommonResult(2000,"成功",courseDetailsVo);
    }

    @Override
    public CommonResult queryAllKeShi(String id) {
        KeShiVo keShiVo = courseDao.queryAllKeShi(id);
        return new CommonResult(2000,"成功",keShiVo);
    }

    //    根据课程id和会员id查询页面是否是讲师发布的
    @Override
    public CommonResult queryAllById(String id,String mobile) {
        List<Course> courses = courseDao.queryAllById(id,mobile);
//        System.out.println("查询course"+courses);
        if (courses.size()>0){
            return new CommonResult(2000,"查询成功",courses);
        }
        return new CommonResult(5000,"查询成功",null);
    }

    @Override
    public CommonResult queryMemberStatus(String id,String mobile) {
        Course course = courseDao.queryMemberStatus(id, mobile);
        if (course!=null){
            return new CommonResult(2000,"查询成功",course);
        }
        return new CommonResult(5000,"查询成功",null);
    }
}